package vn.aptech.leaveManagementSystem.view.admin;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import com.toedter.calendar.JDateChooser;

import vn.aptech.leaveManagementSystem.business.dto.Department;
import vn.aptech.leaveManagementSystem.business.dto.Holiday;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class DemoInsertDepartment extends JFrame{
	private JButton btnAdd;
	private JButton btnCancel;
	private MyModel mymodel=new MyModel();
	AdminForm mainform;
	private JTextField txtDepName;


	public DemoInsertDepartment(AdminForm mainform) {
		this.mainform=mainform;
		initGUI();
		initData();
		initAction();
	}

	private void initAction() {
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Department tempDep=new Department();
				tempDep.setDepartmentName(txtDepName.getText());
				try {
					mymodel.departments.addDepartment(tempDep);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				close();
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
	}

	private void initData() {
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initGUI() {
		setBounds(100, 100, 423, 139);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setTitle("Create New Department");
		getContentPane().setLayout(null);
		setCloseForm();
		
		JLabel lblDepartmentName = new JLabel("Department name:");
		lblDepartmentName.setBounds(10, 26, 103, 14);
		getContentPane().add(lblDepartmentName);
		
		btnAdd = new JButton("Add");
		btnAdd.setIcon(new ImageIcon("icons/gif/16x16/Add.gif"));
		btnAdd.setBounds(100, 71, 89, 23);
		getContentPane().add(btnAdd);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnCancel.setBounds(205, 71, 103, 23);
		getContentPane().add(btnCancel);
		
		txtDepName = new JTextField();
		txtDepName.setBounds(103, 23, 302, 20);
		getContentPane().add(txtDepName);
		txtDepName.setColumns(10);
	}
	
	public void setCloseForm()
	{
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrame jframe=(JFrame) e.getSource();
				mainform.setEnabled(true);
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
	}
	
	public void close()
	{
		toBack();
		dispose();
		mainform.setEnabled(true);
		mainform.updateDataTableDepartment();
	}
	
	
}
